package com.example.demo.test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张述江
 * @date 2023-08-25
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    //swagger2的配置⽂件，这⾥可以配置swagger2的⼀些基本的内容，⽐如扫描的包等等
    @Bean
    public Docket createRestApi() {
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(200).message("成功").responseModel(new ModelRef("Payload")).build());
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .apiInfo(apiInfo()).select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.api"))
                .paths(PathSelectors.any()).build();
        return docket;
    }

    //构建 api⽂档的详细信息函数,注意这⾥的注解引⽤的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页⾯标题
                .title("使⽤ Swagger2 构建RESTful API")
                //创建⼈
                .contact(new Contact("rongdi", "https://www.cnblogs.com/rongdi", "495194630@qq.com"))
                //版本号
                .version("1.0")
                //描述
                .description("api管理").build();
    }
}
